package com.tansun.easycare.rule.monitor.service.impl;

import com.alibaba.fastjson.JSON;
import com.tansun.easycare.core.utils.DateUtils;
import com.tansun.easycare.rule.common.service.impl.RuleBaseServiceImpl;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.monitor.dao.RuleMonitorInfoDao;
import com.tansun.easycare.rule.monitor.entity.RuleMonitorInfo;
import com.tansun.easycare.rule.monitor.service.RuleMonitorInfoService;
import com.tansun.easycare.rule.rulelog.dao.RuleLogsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RuleMonitorInfoServiceImpl extends RuleBaseServiceImpl<RuleMonitorInfoDao, RuleMonitorInfo> implements RuleMonitorInfoService {
    public SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    public SimpleDateFormat format1 = new SimpleDateFormat(
            "yyyyMMdd HH:mm:ss");
    @Autowired
    private RuleLogsDao ruleLogsDao;
    @Autowired
    private RuleMonitorInfoService ruleMonitorInfoService;

    @Override
    public List<String> getProjectCodeList() {
        return this.dao.getProjectCodeList();
    }

    @Override
    public void countNumberForMonitor(Integer monitorDays) {
        Date date = DateUtils.addDays(new Date(),  monitorDays*-1);
        Date beginDate = null;
        try {
            beginDate = getBeginTime(date);
        }catch (Exception e){

        }
        List<RuleMonitorInfo> ruleMonitorInfos = ruleLogsDao.countNumberForMonitor(beginDate);
        if(ruleMonitorInfos!=null && ruleMonitorInfos.size()>0){
            // 获取finalResult值以及次数
            List<RuleMonitorInfo> ruleMonitorInfoList = ruleLogsDao.countNumberForMonitorFinalResult();
            if(ruleMonitorInfoList!=null && ruleMonitorInfoList.size()>0){
                for(RuleMonitorInfo ruleMonitorInfo:ruleMonitorInfos){
                    Map<String,Integer> map=new HashMap<>();
                    for(RuleMonitorInfo ruleMonitorInfo1:ruleMonitorInfoList){
                        if(ruleMonitorInfo.getProjectCode().equals(ruleMonitorInfo1.getProjectCode())
                        && ruleMonitorInfo.getEnterpoint().equals(ruleMonitorInfo1.getEnterpoint())){
                            map.put(ruleMonitorInfo1.getFinalResult(),ruleMonitorInfo1.getExeFinalCount());
                        }
                    }
                    if(map.size()>0){
                        ruleMonitorInfo.setFinalResult(JSON.toJSONString(map));
                    }

                }
            }
        }

        ruleMonitorInfoService.insertBatch(ruleMonitorInfos);
    }

    @Override
    public List<RuleMonitorInfo> findMonitorDataByCode(RuleMonitorInfo ruleMonitorInfo) {
        return this.dao.findMonitorDataByCode(ruleMonitorInfo);
    }

    @Override
    public List<RuleMonitorInfo> findListExistFinalResult(RuleMonitorInfo ruleMonitorInfo) {
        return this.dao.findListExistFinalResult(ruleMonitorInfo);
    }
    //获取某天的最开始时间
    public Date getBeginTime(Date date) throws ParseException {
        String temp = this.format.format(date);
        temp += " 00:00:00";
        Date beginDate = format1.parse(temp);
        return beginDate;
    }
}
